package ru.assa.signalka;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SettingsActivity extends Activity {

    private PendingIntent pi;
	private String LOG_TAG = "SettingsActivity";
	BoxAdapter boxAdapter;
	ArrayList<SettingItem> items = new ArrayList<SettingItem>();
	
	// массивы данных
	//XXX Невозможно работать с ресурсами R.string здесь, только поле onCreate нет контекста
//    SettingItem[] items = {
//    new SettingItem( "Сирена" , "siren_on", "Подача сигналов сиреной вкл/выкл", R.drawable.ic_launcher), 
//    new SettingItem( "Двери", "dor_on", "Концевики дверей вкл/выкл", R.drawable.ic_launcher),
//    };
        
	
	public class SettingItem { //Прикладной класс нельзя вынести из активити т.к. нельзя делать запросы в сервис не из активити
		  public String name;
		  public String command;
		  public String description;
		  public int icon;
		  public boolean check;
		  public boolean checking;
		  public boolean constructing = false;
		  
	    
		   public SettingItem(String name, String command, String description, int icon){
			   this.name = name;
			   this.command = command;
			   this.description = description;
			   this.icon = icon;
			   getCheck();
		   }
		   
		   public SettingItem(Context context, int name, String command, int description, int icon){
			   this.name = context.getResources().getString(name);
			   this.command = command;
			   this.description = context.getResources().getString(description);
			   this.icon = icon;
			   getCheck();
		   }
		   
		   public void getCheck(){
			   this.checking = false;
			   sendToServise(this.command, "?");
		   } //getCheck

		   public void setCheck(boolean val){
			   this.check = val;
			   this.checking = true;
			   if (constructing = true) {
				   boxAdapter.notifyDataSetChanged();
			   } else {
				   constructing = true;
			   }
			   //return this.check;
		   }
		   public String getName(){
			   return this.name;
		   }
		   public String getCommand(){
			   return this.command;
		   }
		   public String getDescription(){
			   return this.description;
		   }
		   public int getIcon(){
			   return this.icon;
		   }
	}//SettingItem
	

  protected void sendToServise(String command , String val){

      pi = createPendingResult(-1, null, 0);
      
  	  Intent intent;
      intent = new Intent(this, BluetoothService.class).putExtra("command", command).putExtra("val", val).putExtra( "pending", pi );
      startService(intent);
} //sendToServise

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  //Intent out_intent = data;
  String command = data.getStringExtra("command");
  String val = data.getStringExtra("val");
  Log.d(LOG_TAG, "requestCode = " + requestCode + ", resultCode = "+ resultCode + ", command = " + command + ", val = " + val);
  for (int i = 0; i < items.size(); i++) {
	  SettingItem p = items.get(i);
 	 if (p.command.equals(command)) {
 		 if ( val.equals("1")) {
 			 p.setCheck(true);
 		 } else if ( val.equals("0")){
 			 p.setCheck(false);
 		 } else {
 			Log.d(LOG_TAG, "VAL NOT FOUND");
 		 }
 		 return;
 	 }
  } 
	Log.d(LOG_TAG, "COMMAND NOT FOUND");
} //onActivityResult

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.set);

    //TODO добавить в класс контекст и получить названия из ресурса
    items.add(new SettingItem( this, R.string.siren_on, "siren_on", R.string.siren_on_desc, R.drawable.ic_launcher));
    items.add(new SettingItem( this, R.string.dor_on, "dor_on", R.string.dor_on_desc, R.drawable.ic_launcher));
    items.add(new SettingItem( this, R.string.st_w, "st_w", R.string.st_w_desc, R.drawable.ic_launcher));
    items.add(new SettingItem( this, R.string.st_g, "st_g", R.string.st_g_desc, R.drawable.ic_launcher));
    items.add(new SettingItem( this, R.string.st_a, "st_a", R.string.st_a_desc, R.drawable.ic_launcher));

//    drawScreen(items);
  
    boxAdapter = new BoxAdapter(this, items);

    ListView lvMain = (ListView) findViewById(R.id.lvSimple);
    lvMain.setAdapter(boxAdapter); 
    
    
    Log.d(LOG_TAG, "--------------------------");
 //   sendToServise("test", "?");
    
  }


}